const fs = require('fs')
const path = require('path')

function createFoldersAndFiles(target, name, skeletonData, atlasData, moreVision) {
  if (!fs.existsSync(target) || !fs.lstatSync(target).isDirectory()) {
    fs.mkdirSync(target)
  }

  const folderPath = path.join(target, name)
  fs.mkdirSync(folderPath)

  const v0Path = path.join(folderPath, 'v0')
  const v3Path = path.join(folderPath, 'v3')
  const v4Path = path.join(folderPath, 'v4')

  fs.writeFileSync(path.join(folderPath, `${name}.json`), skeletonData)
  fs.writeFileSync(path.join(folderPath, `${name}.atlas`), atlasData.replace(/\\n/g, '\n'))
  if (moreVision) {
    fs.mkdirSync(v0Path)
    fs.mkdirSync(v3Path)
    fs.mkdirSync(v4Path)

    fs.writeFileSync(path.join(v0Path, `${name}.json`), skeletonData)
    fs.writeFileSync(path.join(v0Path, `${name}.atlas`), atlasData.replace(/\\n/g, '\n'))
    // fs.writeFileSync(path.join(v3Path, `${name}.json`), skeletonData)
    fs.writeFileSync(path.join(v3Path, `${name}.atlas`), atlasData.replace(/\\n/g, '\n'))
    fs.writeFileSync(path.join(v4Path, `${name}.json`), skeletonData)
    fs.writeFileSync(path.join(v4Path, `${name}.atlas`), atlasData.replace(/\\n/g, '\n'))
  }
}

module.exports = createFoldersAndFiles
